﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.Utils;
using Jd.Api;
using Jd.Api.Request;
using Jd.Api.Response;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using static DXApplicationTangche.DTO.快递打印DTO;
namespace DXApplicationTangche.service
{
    /// <summary>
    /// 快递服务接口
    /// </summary>
    public class 快递Service
    {
        private String url = "https://api.jd.com/routerjson";
        private String appkey = "510466073E35E759DF22B254DDDB7A9D";
        private String appsecret = "bbdd71079232473aa9cf1c1acfe4b8b5";
        private String Token = "aa26f70d2adf40798927be19deae0704ynmy";//    {"access_token":"aa26f70d2adf40798927be19deae0704ynmy","expires_in":31536000,"refresh_token":"06542fe29026411a931c71882aa56d5cdjmz","scope":"snsapi_base","open_id":"nts7K6SM4fwjH4ars5C3iAXJRaD8qOeBztHmF4aJNL4","uid":"3543258491","time":1592033001431,"token_type":"bearer","code":0}
        private String customerCode = "024K708576";
        private String salePlat = "0030001";

        private String senderName = "大连顺秋服装有限公司";
        private String senderAddress = "辽宁省大连市甘井子区营旭路18号";
        private String senderTel = "13940964862";
        private String senderPostcode = "116000";
        public DataRow get交易单号信息(String QR_OTHER14)
        {
            String sql = "SELECT\n" +
                "	QR_NAME ,\n" +
                "	QR_OTHER10  ,\n" +
                "	QR_OTHER26 ,\n" +
                "	QR_OTHER27 ,\n" +
                "	concat( QR_OTHER28, QR_OTHER29, QR_OTHER30, QR_OTHER31 ) AS Address,\n" +
                  "	concat( QR_OTHER28, QR_OTHER29, QR_OTHER30 ) AS area,\n" +
                "	QR_OTHER32  \n" +
                "FROM\n" +
                "	a_bar_code_p \n" +
                "WHERE\n" +
                "	QR_OTHER14 = \"" + QR_OTHER14 + "\"";

            return SQLerp.GetDataRow(sql);
        }
        public DataRow get修改信息(String QR_OTHER14)
        {
            string sql2 = "SELECT\n" +
                "	QR_NAME ,\n" +
                "	QR_OTHER26 ,\n" +
                "	QR_OTHER27 ,\n" +
                "	QR_OTHER28 ,\n" +
                "	QR_OTHER29 ,\n" +
                "	QR_OTHER30 ,\n" +
                "	QR_OTHER31 ,\n" +
               // "	concat( QR_OTHER28, QR_OTHER29, QR_OTHER30, QR_OTHER31 ) AS Address,\n" +
                "	QR_OTHER32  \n" +
                "FROM\n" +
                "	a_bar_code_p \n" +
                "WHERE\n" +
                "	QR_OTHER14 = \"" + QR_OTHER14 + "\"";
            return SQLerp.GetDataRow(sql2);
        }
       

        /// <summary>
        /// 获取青龙运单号接口
        /// </summary>
        public String waybillcode_get()
        {

            String bussiness_no = null;
            IJdClient client = new DefaultJdClient(url, appkey, appsecret);

            EtmsWaybillcodeGetRequest req = new EtmsWaybillcodeGetRequest();

            req.preNum = "1";
            req.customerCode = this.customerCode;
            req.orderType = 0;

            EtmsWaybillcodeGetResponse response = client.Execute(req, Token, DateTime.Now.ToLocalTime());

            List<String> deliveryIdList = this.deSerializeEtmsWaybillcodeGetResponse(response);

            foreach (String str in deliveryIdList)
            {
                bussiness_no = str;
            }
            return bussiness_no;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private List<string> deSerializeEtmsWaybillcodeGetResponse(EtmsWaybillcodeGetResponse response)
        {
            return FunctionHelper.JsonDeserialization<Root>(response.Body).jingdong_etms_waybillcode_get_responce.resultInfo.deliveryIdList;
        }

        /// <summary>
        /// 是否可以京配
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private jingdong_etms_range_check_responce deSerializeEtmsRangeCheckResponseGetResponse(EtmsRangeCheckResponse response)
        {
            return FunctionHelper.JsonDeserialization<jingdong_etms_range_check_responceRoot>(response.Body).jingdong_etms_range_check_responce;
        }


        /// <summary>
        /// 青龙接单接口 
        /// </summary>
        /// <returns></returns>
        public void waybill_send(String order_id,String receive_name, String receive_address, String receive_tel, String post_code,String bussiness_no)
        {
            IJdClient client = new DefaultJdClient(url, appkey, appsecret);

            EtmsWaybillSendRequest req = new EtmsWaybillSendRequest();

            req.deliveryId = bussiness_no;
            req.salePlat = salePlat;
            req.customerCode = customerCode;
            req.orderId = order_id;
            req.senderName = this.senderName;
            req.senderAddress = this.senderAddress;
            req.senderTel = this.senderTel;
            req.senderPostcode = this.senderPostcode;
            //收件人
            req.receiveName = receive_name;
            //收件地址
            req.receiveAddress = receive_address;
            //收件人电话
            req.receiveTel = receive_tel;
             //邮编
            req.postcode = post_code;
            //  特快送
            req.promiseTimeType = 2;

            req.weight = 1;
     
            req.vloumn =200;
            req.packageCount = 1;

            EtmsWaybillSendResponse response = client.Execute(req, Token, DateTime.Now.ToLocalTime());

        }

        /// <summary>
        /// 是否可以京配 
        /// </summary>
        /// <returns></returns>
        public 京配地址DTO range_check(String orderid,String receiveaddress)
        {
            IJdClient client = new DefaultJdClient(url, appkey, appsecret);

            EtmsRangeCheckRequest req = new EtmsRangeCheckRequest();

            req.salePlat = salePlat; 
            req.customerCode = customerCode; 
            req.orderId =orderid;
            req.senderAddress = this.senderAddress;

            req.goodsType = 1;
    
            req.receiveAddress = receiveaddress; 
  
            req.isCod = 0; 
           
            EtmsRangeCheckResponse response = client.Execute(req, Token, DateTime.Now.ToLocalTime());

            jingdong_etms_range_check_responce jingdong_etms_range_check_responce_ = this.deSerializeEtmsRangeCheckResponseGetResponse(response);
            return new 京配地址DTO(jingdong_etms_range_check_responce_.resultInfo);
        }


    }


public class 京配地址DTO
    {
        public String sourcetSortCenterId {get;set;} //	820864	始发分拣中心编码
        public String sourcetSortCenterName { get; set; } // 东莞亚一分拣中心    始发分拣中心名称
        public String targetSortCenterName { get; set; } //  兰州分拨中心 目的分拣中心名称
        public String road { get; set; } //	99E	始发道口号
        public String siteName { get; set; } //	◆  兰州兰工坪营业部 目的站点名称
        public String destinationTabletrolleyCode { get; set; } //	58176	目的分拣中心编码
        public 京配地址DTO(Dictionary<String, String> keyValuePairs) {
            this.sourcetSortCenterId = keyValuePairs.ContainsKey("sourcetSortCenterId") ?keyValuePairs["sourcetSortCenterId"].ToString():"";
            this.sourcetSortCenterName = keyValuePairs.ContainsKey("sourcetSortCenterName") ? keyValuePairs["sourcetSortCenterName"].ToString() : "";
            this.targetSortCenterName = keyValuePairs.ContainsKey("targetSortCenterName") ? keyValuePairs["targetSortCenterName"].ToString() : "";
            this.road = keyValuePairs.ContainsKey("road") ? keyValuePairs["road"].ToString() : ""; 
            this.siteName = keyValuePairs.ContainsKey("siteName") ? keyValuePairs["siteName"].ToString() : "";;
            this.destinationTabletrolleyCode =
                (keyValuePairs.ContainsKey("destinationCrossCode") ? keyValuePairs["destinationCrossCode"].ToString() : "")
                +"-"
                + (keyValuePairs.ContainsKey("destinationTabletrolleyCode") ? keyValuePairs["destinationTabletrolleyCode"].ToString() : "");
            ;
        }
    }

}







